import { getCartList } from './../../api/cart'
export default {
  namespaced: true,
  state: {
    cartNum: 0
  },
  mutations: {
    updatecartNumState (state, payload) {
      state.cartNum = payload
    }
  },
  actions: {
    async updatecartNumAction (context, params) {
      const res = getCartList({
        userid: params.userid
      })
      // console.log(context)
      if (res.data.code === '10020') {
        context.commit('updatecartNumState', 0)
      } else {
        context.cartList = res.data.data
        const num = context.cartList.reduce((sum, item) => {
          return item.flag ? sum + item.num : sum + 0
        }, 0)
        context.commit('updatecartNumState', num)
      }
    }
  }
}
